<html><head><title>guestbook</title><link rel="stylesheet" href="style.css" type="text/css" media="screen" /></head><body>
	<?php
		session_start();
		include_once('mysql_connection.php');  // 包含连接数据库文件
		// start 如果请求是POST，那么处理请求，添加留言
		if($_SERVER['REQUEST_METHOD']=="POST") {
			// start 检测验证码是否正确，如果错误跳转会主页
			if ($_POST['yanzhengma']!=$_SESSION['authnum_session']) {
			?> 
			<meta http-equiv="Refresh" content="3;URL=<?php echo $_SERVER['PHP_SELF'];?>">
			<div style="text-align:center;padding:50px;border-style:dashed;border-width:1px;">
				验证码错误，系统将自动跳转到主页面<br /><br />
				<a href="<?php echo $_SERVER['PHP_SELF'];?>">如果您的浏览器没有自动跳转,请点击这里回到首页</a>
			</div>
			<?php
				exit;
			}
			// end
			$content_text = strip_tags(addslashes($_POST['content_text']));
			$nickname_text = strip_tags(addslashes($_POST['nickname_text']));
			$query = "insert into `guestbook` (`ID`, `content`, `post_time`, `nickname`) values (NULL, '".$content_text."', now(), '".$nickname_text."')";
			$mysql_connection->query($query);
		}
		// end
		// start 设置分页，并且按照页面号码来显示相应的留言
		$count_query = 'select count(*) as `count` from `guestbook`';
		$count_result = $mysql_connection->query($count_query);
		$count_record = $count_result->fetch_assoc();
		$count_record = $count_record['count'];
		$count_page = ceil($count_record/5);
		// start 如果页面数，page的值出现不正确的值，比如负值的处理
		if (isset($_GET['page']) and $_GET['page']>=0) {
			$num_page = $_GET['page'];
			$num_page = floor($num_page);
			if ($num_page > ($count_page-1)) {
				$num_page = $count_page-1;
			}
		} else {
			$num_page = 0;
		}
		// end
		$query = "select * from `guestbook` order by `post_time` desc limit ".($num_page*5).",5";
		$result = $mysql_connection->query($query);
		$num_result = $result->num_rows;
		for ($i=0; $i<$num_result; $i+=1) {
			$row = $result->fetch_assoc();
		?> 
		<div class="connect"><?php echo $row['content'];?><br /><?php echo $row['nickname'];?> 于 <?php echo $row['post_time'];?> 发表</td></tr></table></div>
		<?php
		}
		$result->free(); $mysql_connection->close();
	?> 
	<div id="pre_next_page">
		<?php
      if ($num_page > 0) {
      ?> 
      <a href="<?php echo $_SERVER['PHP_SELF'];?>?page=<?php echo $num_page-1;?>">上一页</a>
      <?php
      }
    ?> 
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<?php
      if ($num_page < ($count_page-1)) {
      ?> 
      <a href="<?php echo $_SERVER['PHP_SELF'];?>?page=<?php echo $num_page+1;?>">下一页</a>
      <?php
      }
	  // end
    ?> 
	</div>
	<!-- start 提交留言的表单 -->
	<div id="form_post"><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
		张贴留言：<br /><textarea name="content_text" id="content_text" cols="75" rows="5"></textarea><br />
		<div id="footer_form_post">
			昵称：<input type="text" name="nickname_text" id="nickname_text" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			验证码：<img src="yanzhengma.php" />&nbsp;&nbsp;  <!-- yanzhengma.php脚本文件本身就代表了一个图片 -->
			<input type="text" name="yanzhengma" id="yanzhengma" />
			<input type="submit" value="提交" />
		</div>
	</form></div>
	<!-- end -->
	<div id="footer">
		<a href="guestbook_login.php">后台管理</a>
	</div>
</body></html>
